
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for view_photo_album_comments.
	''' </summary>
	Public Partial Class view_photo_album_comments
		Inherits BaseWebPage
		Private imgID As Integer = -1
		Protected isLoggedUser As Boolean = False
		Protected nCommentsCount As Double = -1
		Private usrID As Integer = -1
		Private grpID As Integer = -1
		Private usr As BO.User = Nothing
		Private grp As BO.Group = Nothing
		Private isGroupMode As Boolean = False
		Private isModerator As Boolean = False
		Protected htPageConstants As New Hashtable()

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Put user code to initialize the page here
			' this.pageTemplate.PageTitle = PageTitles.VIEW_PHOTO_ALBUM_COMMENTS_PAGE;

			Try
				If LoggedUser = Nothing Then
					Common.Utils.RedirectToLoginPage(Page)
				End If

				Me.initPageTitles(Constants.VIEW_PHOTO_ALBUM_COMMENTS_PAGE, "", Form1, Nothing, htPageConstants)
				Me.initAdvertisements(Form1, Nothing)

				'init and hide user details control
				trUserShortCut.Visible = False

				' init user
				If Request.Params("usr") = Nothing Then
					usr = LoggedUser
				Else
					usrID = Convert.ToInt32(Request.Params("usr"))
					usr = Users.GetUserByID(usrID)
					If usr = Nothing Then
						Throw New Exception("This user doesn't exist")
					End If
				End If

				' init group
				If Request.Params(Constants.GROUP_PARAM) <> Nothing AndAlso Request.Params(Constants.GROUP_PARAM) <> "" Then
					isGroupMode = True
					grpID = Convert.ToInt32(Request.Params(Constants.GROUP_PARAM))
					grp = Groups.GetGroupByID(grpID)
					If grp = Nothing Then
						Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
					End If
					isModerator = (grp.Uid = usr.ID)
				End If

				' init image
				imgID = Convert.ToInt32(Request.Params("img"))

				' init links
				If isGroupMode = True Then
					hlAddComments.Visible = True
					hlBack.Visible = True
					lblLine.Visible = True
					hlBack.Text = (Me.GetPageConstText(htPageConstants, "CONST_GroupBackLink_TEXT")).Replace("[PARAM]", grp.Name)
					If grp.ID = LoggedUser.ID Then
						hlBack.NavigateUrl = "../" + Constants.UPLOAD_GROUP_PHOTO + "?" + Constants.GROUP_PARAM + "=" + grp.ID
					Else
						hlBack.NavigateUrl = "../" + Constants.VIEW_GROUP_PHOTOS + "?" + Constants.GROUP_PARAM + "=" + grp.ID
					End If
ElseIf usr.ID = Me.LoggedUser.ID Then
					hlAddComments.Visible = False
					hlBack.Visible = True
					hlBack.NavigateUrl = "../" + Constants.UPLOAD_PHOTO
					isLoggedUser = True
				End If

				' init Picture
				Dim grpPic As GroupPicture = Nothing
				Dim pic As UserPicture = Nothing
				Dim NewComments As New ArrayList()
				Dim ApprovedComments As New ArrayList()

				If isGroupMode Then
					grpPic = grp.GetPicture(imgID)

					lblDescription.Visible = False

					'init Add Comments link
					hlAddComments.NavigateUrl = "../" + Constants.ADD_PHOTO_ALBUM_COMMENTS + "?img=" + imgID + "&" + Constants.GROUP_PARAM + "=" + grp.ID
					If grpPic <> Nothing Then
						imgLargePhoto.Visible = True
						imgLargePhoto.ImageUrl = Pictures.GetPictureSrcFullURL(grpPic, False, "../")

						If Not Page.IsPostBack Then
							If isModerator AndAlso grp.AutoApproveComments Then
								grpPic.ApproveAllNewPictureComments()
							End If
						End If
					Else
						imgLargePhoto.Visible = False
					End If

					NewComments = grpPic.NewComments
					ApprovedComments = grpPic.Comments
				Else
					pic = usr.GetPicture(imgID)

					' init description
					If pic.Desc <> "" Then
						lblDescription.Text = "<br><br>" + pic.Desc
					Else
						lblDescription.Visible = False
					End If

					'init Add Comments link
					hlAddComments.NavigateUrl = "../" + Constants.ADD_PHOTO_ALBUM_COMMENTS + "?img=" + imgID
					If pic <> Nothing Then
						imgLargePhoto.Visible = True
						imgLargePhoto.ImageUrl = Pictures.GetPictureSrcFullURL(pic, False, "../")

						If Not Page.IsPostBack Then
							If LoggedUser.ID = usr.ID Then
								pic.ApproveAllNewPictureComments()
							End If
						End If
					Else
						imgLargePhoto.Visible = False
					End If
					NewComments = pic.NewComments
					ApprovedComments = pic.Comments
				End If

				If usrID <> -1 Then
					hlAddComments.NavigateUrl += "&usr=" + usrID
				End If

				If isGroupMode Then
					If isModerator Then
						nCommentsCount = NewComments.Count + ApprovedComments.Count
					Else
						nCommentsCount = ApprovedComments.Count
					End If

					trUserShortCut.Visible = False
				Else
					If isLoggedUser OrElse (Not isLoggedUser AndAlso usr.ApproveComments) Then
						nCommentsCount = NewComments.Count + ApprovedComments.Count
					Else
						nCommentsCount = ApprovedComments.Count
					End If

					If usr <> Nothing AndAlso usr.ID <> LoggedUser.ID Then
						ucMemberShortcut.UserID = usr.ID
						trUserShortCut.Visible = True
					End If
				End If

				' init paging

				If isGroupMode Then
					dgPager.RedirectPage = Constants.VIEW_PHOTO_ALBUM_COMMENTS_PAGE + "?img=" + imgID + "&usr=" + usrID + "&" + Constants.GROUP_PARAM + "=" + grp.ID
					dgPagerUp.RedirectPage = Constants.VIEW_PHOTO_ALBUM_COMMENTS_PAGE + "?img=" + imgID + "&usr=" + usrID + "&" + Constants.GROUP_PARAM + "=" + grp.ID
				Else
					dgPager.RedirectPage = Constants.VIEW_PHOTO_ALBUM_COMMENTS_PAGE + "?img=" + imgID + "&usr=" + usrID
					dgPagerUp.RedirectPage = Constants.VIEW_PHOTO_ALBUM_COMMENTS_PAGE + "?img=" + imgID + "&usr=" + usrID
				End If

				dgPager.MaxPageNumber = Convert.ToInt32(Math.Ceiling(nCommentsCount / Constants.MAX_PICTURE_COMMENTS_ON_PAGE))
				dgPager.Action = "-1"
				dgPagerUp.MaxPageNumber = Convert.ToInt32(Math.Ceiling(nCommentsCount / Constants.MAX_PICTURE_COMMENTS_ON_PAGE))
				dgPagerUp.Action = "-1"

				' initialize listing literals
				dgPager.dgListing.TotalCount = Convert.ToInt32(nCommentsCount)
				dgPagerUp.dgListing.TotalCount = Convert.ToInt32(nCommentsCount)
				'initialize current page number
				If Convert.ToInt32(Request.Params("page")) = 0 OrElse dgPager.CurrentPageNumber = 1 Then
					dgPager.CurrentPageNumber = 1
					dgPagerUp.CurrentPageNumber = 1
					ViewState("StartIndex") = 0
				Else
					dgPager.CurrentPageNumber = Convert.ToInt32(Request.Params("page"))
					dgPagerUp.CurrentPageNumber = Convert.ToInt32(Request.Params("page"))
					ViewState("StartIndex") = (dgPager.CurrentPageNumber - 1) * Constants.MAX_PICTURE_COMMENTS_ON_PAGE
				End If

				dgPager.PagesRest = dgPager.MaxPageNumber - dgPager.FirstPageNumber + 1
				dgPager.LastPageNumber = Math.Min(dgPager.PagesRest, Constants.MAX_PAGES_NUMS_ON_PAGE)
				dgPagerUp.PagesRest = dgPagerUp.MaxPageNumber - dgPagerUp.FirstPageNumber + 1
				dgPagerUp.LastPageNumber = Math.Min(dgPagerUp.PagesRest, Constants.MAX_PAGES_NUMS_ON_PAGE)

				dgPager.BeforePageNumberText = ""
				dgPagerUp.BeforePageNumberText = ""

				' init "No comments" label
				If nCommentsCount <= 0 Then
					HideControls()
				End If

				'bind comments
				If Not Page.IsPostBack Then
					BindGrid()
				End If
			Catch
				imgLargePhoto.Visible = False
				HideControls()
				lblNoComments.Text = Constants.ERROR_MESSAGE_MISSING_PARAMETERS
				lblNoComments.Visible = True
				tdMessage.Align = "left"
			End Try
		End Sub

		Protected Function CreateDataSource() As ICollection
			Dim dt As New DataTable()
			Dim curCommentDate As DateTime

			Dim LoggedUsersList As ArrayList = Common.Utils.GetLoggedUsersIDs()

			' List user's inbox messages

			Dim dr As DataRow
			dt.Columns.Add(New DataColumn("SenderID", GetType(Integer)))
			dt.Columns.Add(New DataColumn("SenderName", GetType(String)))
			dt.Columns.Add(New DataColumn("SenderImageURL", GetType(String)))
			dt.Columns.Add(New DataColumn("OnlineImg", GetType(String)))
			dt.Columns.Add(New DataColumn("MemberURL", GetType(String)))
			dt.Columns.Add(New DataColumn("Date", GetType(String)))
			dt.Columns.Add(New DataColumn("Time", GetType(String)))
			dt.Columns.Add(New DataColumn("Comment", GetType(String)))
			dt.Columns.Add(New DataColumn("CommentID", GetType(String)))
			dt.Columns.Add(New DataColumn("ButtonsVisibility", GetType(Boolean)))
			dt.Columns.Add(New DataColumn("RejectText", GetType(String)))
			dt.Columns.Add(New DataColumn("ButtonRejectVisibility", GetType(Boolean)))
			dt.Columns.Add(New DataColumn("SenderNameFull", GetType(String)))

			If isGroupMode Then
				Dim pic As GroupPicture = grp.GetPicture(imgID)
				Dim newCommentList As ArrayList, approvedCommentList As ArrayList, allComments As ArrayList
				allComments = New ArrayList()
				newCommentList = pic.NewComments
				approvedCommentList = pic.Comments
				If grp.Uid = LoggedUser.ID Then
					allComments = newCommentList
				End If
				Dim i As Integer = 0
				While i < approvedCommentList.Count
					allComments.Add(approvedCommentList(i))
					System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
				End While

				If allComments.Count <= 0 Then
					dgPager.FirstPageNumber = 1
					dgPager.LastPageNumber = 1
					dgPager.MaxPageNumber = 1
					dgPager.PagesRest = 1
					dgPager.dgListing.StartIndex = 0
					dgPager.dgListing.EndIndex = 0
					dgPagerUp.FirstPageNumber = 1
					dgPagerUp.LastPageNumber = 1
					dgPagerUp.MaxPageNumber = 1
					dgPagerUp.PagesRest = 1
					dgPagerUp.dgListing.StartIndex = 0
					dgPagerUp.dgListing.EndIndex = 0
					dgMessages.AllowSorting = False
					dgPagerUp.Visible = False
					dgPager.Visible = False
				Else
					Dim startIndex As Integer = Convert.ToInt32(ViewState("StartIndex"))
					Dim endIndex As Integer = startIndex + Constants.MAX_PICTURE_COMMENTS_ON_PAGE
					Dim t As Integer = 0
					Dim counter As Integer = 0

					endIndex = Math.Min(endIndex, allComments.Count)
					t = (endIndex - startIndex)
					dgPager.dgListing.StartIndex = startIndex + 1
					dgPager.dgListing.EndIndex = endIndex
					dgPagerUp.dgListing.StartIndex = startIndex + 1
					dgPagerUp.dgListing.EndIndex = endIndex

					' initialize listing literals
					dgPager.dgListing.TotalCount = Convert.ToInt32(allComments.Count)
					dgPager.dgListing.UpdateControlsTexts()
					dgPagerUp.dgListing.TotalCount = Convert.ToInt32(allComments.Count)
					dgPagerUp.dgListing.UpdateControlsTexts()
					'initialize current page number


					Dim temp As UserPicture
					Dim curSender As User
					' List group's image NEW comments
					For Each picComment As PictureComment In allComments
						If counter >= startIndex AndAlso counter < endIndex Then
							dr = dt.NewRow()
							curCommentDate = picComment.[Date]
							curSender = Users.GetUserByID(picComment.PostedByUserID)
							dr(0) = picComment.PostedByUserID
							dr(1) = getBeginning(HttpUtility.HtmlEncode(curSender.Name), Constants.MAX_NAME_LENGTH_ABOVE_IMAGE)
							dr(12) = curSender.Name
							temp = curSender.DefaultPicture
							If temp <> Nothing Then
								dr(2) = Pictures.GetPictureSrcFullURL(temp, True, "../")
							Else
								dr(2) = Constants.DEFAULT_THUMBNAIL_IMAGE_URL
							End If

							If LoggedUsersList.Contains(curSender.ID) Then
								dr(3) = "../images/online_status.gif"
							Else
								dr(3) = "../images/spacer.gif"
							End If

							'if(curSender.Status == Users.STATUS_ACTIVE)
							If curSender.Status <> Users.STATUS_LOCKED Then
								dr(4) = "../" + Constants.MEMBERS + "?id=" + picComment.PostedByUserID
							End If

							dr(5) = curCommentDate.DayOfWeek + ", " + Constants.ARR_MONTHS(curCommentDate.Month - 1) + " " + curCommentDate.Day + ", " + curCommentDate.Year
							dr(6) = curCommentDate.ToShortTimeString()
							dr(7) = picComment.Comment
							dr(8) = picComment.ID
							If picComment.CommentStatus = PictureComment.Status.pcsNew Then
								dr(9) = True
								dr(10) = "Reject"
							Else
								dr(9) = False
								dr(10) = "Delete"
							End If

							If LoggedUser.ID = grp.Uid OrElse LoggedUser.ID = picComment.PostedByUserID Then
								dr(11) = True
							Else
								dr(11) = False
							End If
							dt.Rows.Add(dr)
						End If
						System.Math.Max(System.Threading.Interlocked.Increment(counter),counter - 1)
					Next
				End If
			Else
				Dim pic As UserPicture = usr.GetPicture(imgID)
				Dim newCommentList As ArrayList, approvedCommentList As ArrayList, allComments As ArrayList
				allComments = New ArrayList()
				newCommentList = pic.NewComments
				approvedCommentList = pic.Comments
				If (usr.ID = LoggedUser.ID) OrElse (usr.ID <> LoggedUser.ID AndAlso usr.ApproveComments) Then
					allComments = newCommentList
				End If
				Dim i As Integer = 0
				While i < approvedCommentList.Count
					allComments.Add(approvedCommentList(i))
					System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
				End While

				If allComments.Count <= 0 Then
					dgPager.FirstPageNumber = 1
					dgPager.LastPageNumber = 1
					dgPager.MaxPageNumber = 1
					dgPager.PagesRest = 1
					dgPager.dgListing.StartIndex = 0
					dgPager.dgListing.EndIndex = 0
					dgPagerUp.FirstPageNumber = 1
					dgPagerUp.LastPageNumber = 1
					dgPagerUp.MaxPageNumber = 1
					dgPagerUp.PagesRest = 1
					dgPagerUp.dgListing.StartIndex = 0
					dgPagerUp.dgListing.EndIndex = 0
					dgMessages.AllowSorting = False
					dgPagerUp.Visible = False
					dgPager.Visible = False
				Else
					Dim startIndex As Integer = Convert.ToInt32(ViewState("StartIndex"))
					Dim endIndex As Integer = startIndex + Constants.MAX_PICTURE_COMMENTS_ON_PAGE
					Dim t As Integer = 0
					Dim counter As Integer = 0

					endIndex = Math.Min(endIndex, allComments.Count)
					t = (endIndex - startIndex)
					dgPager.dgListing.StartIndex = startIndex + 1
					dgPager.dgListing.EndIndex = endIndex
					dgPagerUp.dgListing.StartIndex = startIndex + 1
					dgPagerUp.dgListing.EndIndex = endIndex

					' initialize listing literals
					dgPager.dgListing.TotalCount = Convert.ToInt32(allComments.Count)
					dgPager.dgListing.UpdateControlsTexts()
					dgPagerUp.dgListing.TotalCount = Convert.ToInt32(allComments.Count)
					dgPagerUp.dgListing.UpdateControlsTexts()
					'initialize current page number

					Dim temp As UserPicture
					Dim curSender As User
					' List user's image NEW comments
					For Each picComment As PictureComment In allComments
						If counter >= startIndex AndAlso counter < endIndex Then
							dr = dt.NewRow()
							curCommentDate = picComment.[Date]
							curSender = Users.GetUserByID(picComment.PostedByUserID)
							dr(0) = picComment.PostedByUserID
							dr(1) = getBeginning(HttpUtility.HtmlEncode(curSender.Name), Constants.MAX_NAME_LENGTH_ABOVE_IMAGE)
							dr(12) = curSender.Name
							temp = curSender.DefaultPicture
							If temp <> Nothing Then
								dr(2) = Pictures.GetPictureSrcFullURL(temp, True, "../")
							Else
								dr(2) = Constants.DEFAULT_THUMBNAIL_IMAGE_URL
							End If

							If LoggedUsersList.Contains(curSender.ID) Then
								dr(3) = "../images/online_status.gif"
							Else
								dr(3) = "../images/spacer.gif"
							End If

							'if(curSender.Status == Users.STATUS_ACTIVE)
							If curSender.Status <> Users.STATUS_LOCKED Then
								dr(4) = "../" + Constants.MEMBERS + "?id=" + picComment.PostedByUserID
							End If

							dr(5) = curCommentDate.DayOfWeek + ", " + Constants.ARR_MONTHS(curCommentDate.Month - 1) + " " + curCommentDate.Day + ", " + curCommentDate.Year
							dr(6) = curCommentDate.ToShortTimeString()
							dr(7) = picComment.Comment
							dr(8) = picComment.ID
							If picComment.CommentStatus = PictureComment.Status.pcsNew Then
								dr(9) = True
								dr(10) = "Reject"
							Else
								dr(9) = False
								dr(10) = "Delete"
							End If

							If LoggedUser.ID = usrID OrElse LoggedUser.ID = picComment.PostedByUserID Then
								dr(11) = True
							Else
								dr(11) = False
							End If
							dt.Rows.Add(dr)
						End If
						System.Math.Max(System.Threading.Interlocked.Increment(counter),counter - 1)
					Next
				End If
			End If

			Dim dv As New DataView(dt)
			Return dv
		End Function

		Protected Sub BindGrid()
			dgMessages.DataSource = CreateDataSource()
			dgMessages.DataBind()
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			AddHandler Me.dgMessages.ItemCommand, AddressOf Me.dgMessages_ItemCommand

		End Sub
#End Region

		Private Sub dgMessages_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
			Dim lbPushed As LinkButton = DirectCast(e.CommandSource, LinkButton)
			Dim cmd As String = e.CommandName

			If isGroupMode Then
				Dim pic As GroupPicture = grp.GetPicture(imgID)
				Select Case cmd
					Case "Reject"
						' reject/remove comment
						pic.RemoveComment(Convert.ToInt32(e.CommandArgument))
						Exit Select
					Case "Approve"
						pic.ApproveComment(Convert.ToInt32(e.CommandArgument))
						Exit Select
				End Select
			Else
				Dim pic As UserPicture = usr.GetPicture(imgID)
				Select Case cmd
					Case "Reject"
						' reject/remove comment
						pic.RemoveComment(Convert.ToInt32(e.CommandArgument))
						Exit Select
					Case "Approve"
						pic.ApproveComment(Convert.ToInt32(e.CommandArgument))
						Exit Select
				End Select
			End If

			BindGrid()
		End Sub

		Private Sub HideControls()
			dgMessages.Visible = False
			lblNoComments.Visible = True
			trPagerUp.Visible = False
			trPager.Visible = False
		End Sub

		====================
		' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable Form1.
		====================
		Public ReadOnly Property Form1() As System.Web.UI.HtmlControls.HtmlForm
			Get
				Return Migrated_Form1
			End Get
		End Property
		'set { Migrated_Form1 = value; }
	End Class
End Namespace






